/**
 * Created by wangshaohua on 2017/12/8.
 */
moduleStudyMap.controller("SetStudyMapRouteController", function ($$CambioConfig, $scope, $log, $state, LoadingService, DialogService,
                                                                  EnumService, ConstantService, StudyMapRouteResource) {
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
        getStudyMapRoute();
    });

    var getStudyMapRoute = function () {
        if (StringUtils.isNlOrUndOrEmpty($scope.currentStudyMap.id)) {
            $scope.setStudyMapRoute = {};
        } else {
            //LoadingService.show();
            StudyMapRouteResource.findByStudyMapId({studyMapId: $scope.currentStudyMap.id}, function (studyMapRouteResult) {
                $log.debug("返回的数据", studyMapRouteResult);
                $scope.setStudyMapRoute = studyMapRouteResult;
                //LoadingService.hide();
            }, function () {
                //LoadingService.hide();
            });
        }
    };

    $scope.queryStudyMapRoute = function () {
        //LoadingService.show();
        StudyMapRouteResource.findByKeyword({}, {keyword: $scope.routeKey}, function (routeResult) {
            $log.debug("H5项目匹配结果", routeResult);
            $scope.routeResult = routeResult;
            //LoadingService.hide();
        });
    };

    $scope.unBind = function () {
        Alert.confirm(function () {
            var bean = {
                studyMapId: $scope.currentStudyMap.id,
                studyMapRouteStyleId: $scope.setStudyMapRoute.id
            };
            StudyMapRouteResource.studyMapAndRouteUnBind({}, bean, function () {
                //$log.debug("清理用户信息");
                DialogService.close();
                Alert.success();
                //LoadingService.hide();
            });
        });
    }

    $scope.setCurrentRoute = function (route) {
        $scope.setStudyMapRoute = {
            id: route.id,
            name: route.name,
            code: route.code
        };
    };

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.submit = function () {
        Alert.confirm(function () {
            var bean = {
                studyMapId: $scope.currentStudyMap.id,
                studyMapRouteStyleId: $scope.setStudyMapRoute.id
            };
            StudyMapRouteResource.studyMapAndRouteBind({}, bean, function () {
                DialogService.close();
                Alert.success();
                //LoadingService.hide();
            });
        });
    };
});